import Vue from 'vue'
import VueRouter from 'vue-router'
import HomePage from "@/views/HomePage.vue";
import LoginPage from "@/views/LoginPage.vue";
import MaterialSavePage from "@/views/material/MaterialSavePage.vue";
import MaterialPage from "@/views/material/MaterialPage.vue";
import InventoryChangePage from "@/views/inventory/InventoryChangePage.vue";
import InventoryCheckPage from "@/views/inventory/InventoryCheckPage.vue";


Vue.use(VueRouter)

const routes = [
    {path: '/login', component: LoginPage},
    {path: '/', component: HomePage, children:[
            {path: '/material/save', component: MaterialSavePage},
            {path: '/material/page', component: MaterialPage},
            {path: '/inventory/change', component: InventoryChangePage},
			{path: '/inventory/check', component: InventoryCheckPage},
        ]}
]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})



router.beforeEach((to, from, next) => {
    if (to.path == '/login') {
        next()
        return
    }
    Vue.prototype.$http.get('/user/session')
        .then((result)=> {
            console.log('then')
            if (result.data.code == 10000) {
                next()
            } else if (result.data.code == 10007) {
                next({ path: '/login' })
            }
        })
        .catch((error)=>{
            console.log('catch')
        })
});


export default router
